﻿using DryIoc;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using StarryEdge.Core.Enums;
using StarryEdge.Core.Models;
using StarryEdge.Core.Parameter;
using StarryEdge.Menu.User.ViewModels.Dialog;
using StarryEdge.Menu.User.Views.Dialog;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace StarryEdge.Menu.User.ViewModels
{
    public class UserEditViewModel
    {
        private IUserEntity _userEentity;
        public ObservableCollection<UserModel> Users { get; set; }
        private ITemporaryParameter _temporaryParameter;
        public UserEditViewModel(IUserEntity userEentity, ITemporaryParameter temporaryParameter)
        {
            _temporaryParameter = temporaryParameter;
            _userEentity = userEentity;
            Users = new ObservableCollection<UserModel>();
            foreach (UserModel user in userEentity.GetUsers())
            {
                Users.Add(user);
            }
        }

        public DelegateCommand New => new DelegateCommand(async () =>
        {
            _temporaryParameter.UserEdit = true;
            await MaterialDesignThemes.Wpf.DialogHost.Show(new AddUser() { DataContext = new AddUserPopupDialog(_userEentity, default) }, Core.Enums.PopUpTypes.Global.ToString());
            _temporaryParameter.UserEdit = false;
            Users.Clear();
            foreach (UserModel user in _userEentity.GetUsers())
            {
                Users.Add(user);
            }
        });

        public DelegateCommand<UserModel> MouseDoubleClick => new DelegateCommand<UserModel>(async sender =>
        {
            if (sender != null)
            {
                _temporaryParameter.UserEdit = true;
                await MaterialDesignThemes.Wpf.DialogHost.Show(new AddUser() { DataContext = new AddUserPopupDialog(_userEentity, sender) }, Core.Enums.PopUpTypes.Global.ToString());
                _temporaryParameter.UserEdit = false;
            }
        });

        public DelegateCommand<ListView> SelectDelete => new DelegateCommand<ListView>(sender =>
        {
            foreach (var item in sender.SelectedItems)
            {
                _userEentity.RemoveUser(item as UserModel);
            }
            _userEentity.Save();
            Users.Clear();
            foreach (UserModel user in _userEentity.GetUsers())
            {
                Users.Add(user);
            }
        });
    }
}
